import {Directive, ElementRef, HostListener, Input} from '@angular/core';
import {Platform} from 'ionic-angular';

import {ToastService} from '../../services/common/toast.service';

/**
 * Generated class for the InputContentLengthDirective directive.
 *
 * See https://angular.io/api/core/Directive for more info on Angular
 * Directives.
 */
@Directive({
    selector: 'ion-input[input-content-length]' // Attribute selector
})
export class InputContentLengthDirective {

    private titleLengthErr:boolean;
    @Input('input-content-length') maxLength: number;

    constructor(public el: ElementRef, private platform: Platform,private toastService:ToastService) {
        console.log('Hello InputContentLengthDirective Directive');
    }

    @HostListener('input', ['$event'])

    onInput($event) {
        console.log('input');
        console.log($event);
        if($event.target['value'].length>=this.maxLength&&!this.titleLengthErr){
            this.titleLengthErr=true;
            this.toastService.showToast('日程标题长度不能大于30')
        }
    }

    @HostListener('blur', ['$event'])

    onBlur($event) {
        this.titleLengthErr=false;
    }
}
